package pers.zh.parking.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import pers.zh.parking.R;
import pers.zh.parking.di.component.DaggerSignupComponent;
import pers.zh.parking.mvp.contract.SignupContract;
import pers.zh.parking.mvp.presenter.SignupPresenter;
import pers.zh.parking.utils.CommonTextWatcher;
import pers.zjc.commonlibs.util.ToastUtils;

import static com.jess.arms.utils.Preconditions.checkNotNull;

public class SignupActivity extends BaseActivity<SignupPresenter>
        implements SignupContract.View, View.OnClickListener {

    @BindView(R.id.ivLeft)
    ImageView ivLeft;
    @BindView(R.id.toolbar_left)
    RelativeLayout toolbarLeft;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.etAccount)
    EditText etAccount;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.btn_sign_up)
    TextView tvSignUp;
    @BindView(R.id.et_username)
    EditText etUsername;

    private String account;
    private String password;
    private String userName;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerSignupComponent //如找不到该类,请编译一下项目
                              .builder().appComponent(appComponent).view(this).build().inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_signup; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        initView();
    }

    private void initView() {
        tvTitle.setText("用户注册");
        ivLeft.setOnClickListener(this);
        tvSignUp.setOnClickListener(this);
        configEditText(etUsername);
        configEditText(etAccount);
        configEditText(etPwd);
    }

    private void configEditText(EditText editText) {
        CommonTextWatcher watcher = new CommonTextWatcher(editText) {
            @Override
            public void afterChanged(String s) {
                switch (editText.getId()) {
                    case R.id.etAccount:
                        account = etAccount.getText().toString();
                        break;
                    case R.id.etPwd:
                        password = etPwd.getText().toString();
                        break;
                    case R.id.et_username:
                        userName = etUsername.getText().toString();
                        break;
                    default:
                        break;
                }
            }
        };
        editText.addTextChangedListener(watcher);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastUtils.showShort(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivLeft:
                finish();
                break;
            case R.id.btn_sign_up:
                mPresenter.signUp(account, password, userName);
                break;
            default:
                break;
        }
    }

    @Override
    public void signUpSuccess() {
        showMessage("注册成功");
        finish();
    }
}
